package pers.xiaoliang.concurrency.example.count;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import pers.xiaoliang.concurrency.annotation.ThreadSafe;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @version v1.0
 * @description: 计数器例子2 使用AtomicIntegerFieldUpdater
 * @author: xiaoliang.liu
 * @date: 2018/7/26 21:31
 */
@Slf4j
@ThreadSafe
public class CountExample4 {

    private static AtomicIntegerFieldUpdater<CountExample4> updater = AtomicIntegerFieldUpdater.newUpdater(CountExample4.class, "count");

    @Getter
    public volatile int count = 100;

    private static CountExample4 countExample4 = new CountExample4();

    public static void main(String[] args) {
        if (updater.compareAndSet(countExample4, 100, 120)) {
            log.info(String.valueOf(countExample4.getCount()));
        }
        if (updater.compareAndSet(countExample4, 100, 120)) {
            log.info(String.valueOf(countExample4.getCount()));
        } else {
            log.info("fail{}", String.valueOf(countExample4.getCount()));
        }

    }
}
